package net.zf.dynamic.threadpool.bean;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.List;


/**
 * 动态线程池上下文
 * @author tengyd
 */
public class DynamicThreadPoolContext {

    private String applicationName;

    private final Map<String, ThreadPoolExecutor> threadPoolExecutorMap;

    public DynamicThreadPoolContext(String applicationName, Map<String, ThreadPoolExecutor> threadPoolExecutorMap) {
        this.threadPoolExecutorMap = threadPoolExecutorMap;
        this.applicationName = applicationName;
    }

    /**
     * 获取应用所有线程列表
     * @return
     */
    public List<ThreadPoolConfigEntity> queryThreadPoolList(){
        Set<String> threaPoolNameSet = threadPoolExecutorMap.keySet();

        List<ThreadPoolConfigEntity> threaPoolConfigEntityList = new ArrayList<ThreadPoolConfigEntity>(threaPoolNameSet.size());

        for(String threadPoolName : threaPoolNameSet){
            ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(threadPoolName);

            ThreadPoolConfigEntity threadPoolConfig = new ThreadPoolConfigEntity();
            threadPoolConfig.setApplicationName(applicationName);
            threadPoolConfig.setThreadPoolName(threadPoolName);

            threadPoolConfig.setCorePoolSize(threadPoolExecutor.getCorePoolSize());
            threadPoolConfig.setMaxPoolSize(threadPoolExecutor.getMaximumPoolSize());

            threadPoolConfig.setPoolSize(threadPoolConfig.getPoolSize());
            threadPoolConfig.setActiveCount(threadPoolConfig.getActiveCount());

            threadPoolConfig.setQueueType(threadPoolExecutor.getQueue().getClass().getTypeName());
            threadPoolConfig.setQueueSize(threadPoolExecutor.getQueue().size());
            threadPoolConfig.setQueueRemainSize(threadPoolExecutor.getQueue().remainingCapacity());

            threaPoolConfigEntityList.add(threadPoolConfig);
        }

        return threaPoolConfigEntityList;
    }

    /**
     * 查询单个线程池配置
     * @param threadPoolName
     * @return
     */
    public ThreadPoolConfigEntity getThreadPoolConfigEntity(String threadPoolName){
        ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(threadPoolName);
        if(threadPoolExecutor == null){
            return null;
        }
        ThreadPoolConfigEntity threadPoolConfig = new ThreadPoolConfigEntity();
        threadPoolConfig.setApplicationName(applicationName);
        threadPoolConfig.setThreadPoolName(threadPoolName);

        threadPoolConfig.setCorePoolSize(threadPoolExecutor.getCorePoolSize());
        threadPoolConfig.setMaxPoolSize(threadPoolExecutor.getMaximumPoolSize());

        threadPoolConfig.setPoolSize(threadPoolConfig.getPoolSize());
        threadPoolConfig.setActiveCount(threadPoolConfig.getActiveCount());

        threadPoolConfig.setQueueType(threadPoolExecutor.getQueue().getClass().getTypeName());
        threadPoolConfig.setQueueSize(threadPoolExecutor.getQueue().size());
        threadPoolConfig.setQueueRemainSize(threadPoolExecutor.getQueue().remainingCapacity());


        return threadPoolConfig;

    }

    /**
     * 修改线程池配置
     * @param threadPoolConfig
     * @return
     */
    public boolean updateThreadPoolConfig(ThreadPoolConfigEntity threadPoolConfig){
        ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(threadPoolConfig.getThreadPoolName());
        if(threadPoolExecutor == null){
            return false;
        }

        threadPoolExecutor.setCorePoolSize(threadPoolConfig.getCorePoolSize());
        threadPoolExecutor.setMaximumPoolSize(threadPoolConfig.getMaxPoolSize());

        threadPoolExecutorMap.put(threadPoolConfig.getApplicationName(), threadPoolExecutor);
        return true;
    }



}
